//
//  ContainerController.m
//  ActiveDeck
//
//  Created by Laura McBride on 9/28/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ContainerController.h"
#import "AD_ContainerOB.h"
#import "GDataXMLNode.h"
#import "AD_Note.h"
#import "AD_Notice.h"
#import "AD_PhotoNote.h"

@implementation ContainerController
@synthesize ContainOB;

-(id)init:(int)PresentID
UserFolderName:(NSString*)UserFolderName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *docDir = [paths objectAtIndex:0];
	NSString *PresentDir = [NSString stringWithFormat:@"%@/%d", docDir, PresentID];
	NSString *UserFileDir = [NSString stringWithFormat:@"%@/Container/%@", PresentDir,UserFolderName];
    NSString *xmlFilePath = [NSString stringWithFormat:@"%@/ContainerInfo.xml", UserFileDir];
    
    NSFileManager *nsFM = [NSFileManager defaultManager];
    BOOL isDir = YES;

	if(![nsFM fileExistsAtPath:UserFileDir isDirectory:&isDir])
		[nsFM createDirectoryAtPath:UserFileDir withIntermediateDirectories:YES attributes:nil error:nil];
    
    if(![nsFM fileExistsAtPath:xmlFilePath isDirectory:FALSE])
        [self InitialXMLStructure:xmlFilePath];
    
    [self loadContainerXML:xmlFilePath];
    ContainOB.Path_Container = UserFileDir;
    
    return self;
}

-(void)loadContainerXML:(NSString*)xmlFilePath
{
    ContainOB = [[AD_ContainerOB alloc]Init];
    
    NSData *xData = [[NSMutableData alloc]initWithContentsOfFile:xmlFilePath];
    GDataXMLDocument *xDoc = [[GDataXMLDocument alloc] initWithData:xData options:0 error:nil];
    
    NSArray *NoticesSet = [xDoc nodesForXPath:@"//ContainerInfo/Notices/Notice" error:nil];
    NSArray *NotesSet = [xDoc nodesForXPath:@"//ContainerInfo/Notes/Note" error:nil];
    NSArray *PhotosSet = [xDoc  nodesForXPath:@"//ContainerInfo/Photos/Photo" error:nil];
    
    for (GDataXMLElement *notice in NoticesSet)
    {
        AD_Notice *adNotice = [AD_Notice alloc];
        adNotice.Txt_Notice = [[notice attributeForName:@"Text"]stringValue];
        adNotice.Txt_Time = [[notice attributeForName:@"Time"]stringValue];
        adNotice.ID = [[[notice attributeForName:@"ID"]stringValue]intValue];
        
        [ContainOB.Array_Notice addObject:adNotice];
        NSLog(@"Notice count = %d", [ContainOB.Array_Notice count]);
        [adNotice release];
    }
    for (GDataXMLElement *note in NotesSet)
    {
        AD_Note *adNote = [AD_Note alloc];
        adNote.Txt_Note = [[note attributeForName:@"Text"]stringValue];
        adNote.SlideIndex = [[[note attributeForName:@"SlideIndex"]stringValue]intValue];

        [ContainOB.Array_Note addObject:adNote];
        [adNote release];
    }
    
    for (GDataXMLElement *photo in PhotosSet)
    {
        AD_PhotoNote *adPhoto = [AD_PhotoNote alloc];
        adPhoto.Txt_Note = [[photo attributeForName:@"Text"]stringValue];
        adPhoto.Path_Image = [[photo attributeForName:@"ImgPath"]stringValue];
        adPhoto.SlideIndex = [[[photo attributeForName:@"SlideIndex"]stringValue]intValue];
        
        [ContainOB.Array_Photo addObject:adPhoto];
        [adPhoto release];
    }
    
    [xDoc release];
    [xData release];
}

-(void)InitialXMLStructure:(NSString *)xmlFilePath
{
    GDataXMLElement *ContainerInfo = [GDataXMLNode elementWithName:@"ContainerInfo"];
    GDataXMLElement *NoticeSet = [GDataXMLNode elementWithName:@"Notices"];
    GDataXMLElement *NoteSet = [GDataXMLNode elementWithName:@"Notes"];
    GDataXMLElement *PhotoSet = [GDataXMLNode elementWithName:@"Photos"];
    
    [ContainerInfo addChild:NoticeSet];
    [ContainerInfo addChild:NoteSet];
    [ContainerInfo addChild:PhotoSet];
    
    GDataXMLDocument *xDoc = [[GDataXMLDocument alloc] initWithRootElement:ContainerInfo];
    NSData *xData = xDoc.XMLData;
    [xData writeToFile:xmlFilePath atomically:TRUE];
    
    [xDoc release];
}

-(void)SaveContainerOBtoXML
{
    GDataXMLElement *ContainerInfo = [GDataXMLNode elementWithName:@"ContainerInfo"];
    GDataXMLElement *NoticeSet = [GDataXMLNode elementWithName:@"Notices"];
    GDataXMLElement *NoteSet = [GDataXMLNode elementWithName:@"Notes"];
    GDataXMLElement *PhotoSet = [GDataXMLNode elementWithName:@"Photos"];
    
    for (AD_Notice *adNotice in ContainOB.Array_Notice)
    {
        GDataXMLElement *Notice = [GDataXMLElement elementWithName:@"Notice"];
        GDataXMLNode *ID = [GDataXMLNode attributeWithName:@"ID" stringValue:[NSString stringWithFormat:@"%d", adNotice.ID]];
        GDataXMLNode *Text = [GDataXMLNode attributeWithName:@"Text" stringValue:adNotice.Txt_Notice];
        GDataXMLNode *Time = [GDataXMLNode attributeWithName:@"Time" stringValue:adNotice.Txt_Time];
        
        [Notice addChild:ID];
        [Notice addChild:Time];
        [Notice addChild:Text];
        [NoticeSet addChild:Notice];
    }
    
    for (AD_Note *adNote in ContainOB.Array_Note)
    {
        GDataXMLElement *Note = [GDataXMLElement elementWithName:@"Note"];
        GDataXMLNode *SlideIndex = [GDataXMLNode attributeWithName:@"SlideIndex" stringValue:[NSString stringWithFormat:@"%d", adNote.SlideIndex]];
        GDataXMLNode *Text = [GDataXMLNode attributeWithName:@"Text" stringValue:adNote.Txt_Note];
        
        [Note addChild:SlideIndex];
        [Note addChild:Text];
        [NoteSet addChild:Note];
    }
    
    for (AD_PhotoNote *adPhoto in ContainOB.Array_Photo)
    {
        GDataXMLElement *Photo = [GDataXMLElement elementWithName:@"Photo"];
        GDataXMLNode *SlideIndex = [GDataXMLNode attributeWithName:@"SlideIndex" stringValue:[NSString stringWithFormat:@"%d", adPhoto.SlideIndex]];
        GDataXMLNode *Text = [GDataXMLNode attributeWithName:@"Text" stringValue:adPhoto.Txt_Note];
        GDataXMLNode *ImgPath = [GDataXMLNode attributeWithName:@"ImgPath" stringValue:adPhoto.Path_Image];
        
        [Photo addChild:SlideIndex];
        [Photo addChild:ImgPath];
        [Photo addChild:Text];
        [PhotoSet addChild:Photo];
    }
    
    [ContainerInfo addChild:NoticeSet];
    [ContainerInfo addChild:NoteSet];
    [ContainerInfo addChild:PhotoSet];
    
    GDataXMLDocument *xDoc = [[GDataXMLDocument alloc] initWithRootElement:ContainerInfo];
    NSData *xData = xDoc.XMLData;
    [xData writeToFile:[NSString stringWithFormat:@"%@/ContainerInfo.xml", ContainOB.Path_Container] atomically:TRUE];
    
    [xDoc release];
}


-(void)dealloc{
    [ContainOB release];
    [super dealloc];
}
@end
